package com.lkm.app_ui.m.Imple.order_img;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;

import com.lkm.app_ui.AppConfig;
import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.m.order_img.OnGoodImageTackEvent;
import com.lkm.frame.file.FileHelper;
import com.lkm.frame.otto.LinkBus;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

import androidx.annotation.Nullable;



class ImgFiler {

    public static void ayncDeleteTemp(OrderEntity.GoodEntity goodEntity) {
        AppConst.getTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                deleteTemp(goodEntity);
            }
        });
    }

    public static void ayncDeleteAllTemp() {
        AppConst.getTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                deleteAllTemp();
            }
        });
    }


    public static void ayncCpToUp(OrderEntity orderEntity, Runnable runnable) {
        AppConst.getTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                cpToUp(orderEntity);
                if(runnable!=null)runnable.run();
            }
        });
    }

    public static void ayncSave(OrderEntity.GoodEntity goodEntity, byte[] data) {

        AppConst.getTaskExecutor().execute(new Runnable() {
            @Override
            public void run() {
                save_(goodEntity, data);
            }
        });
    }

    public synchronized static @Nullable
    File[] getUpSZFiles() {
        return new File(AppConfig.DataFile.orderImgUpSZFileDir).listFiles();
    }

    public synchronized static @Nullable File[] getUpFiles() {
        return new File(AppConfig.DataFile.orderImgUpFileDir).listFiles();
    }


    public volatile static boolean isWriteToUp = false;


    private synchronized static void cpToUp(OrderEntity orderEntity) {

        isWriteToUp = true;
        List<OrderEntity.GoodEntity> goodEntities = orderEntity.getGoods();
        if(goodEntities!=null)
        for (OrderEntity.GoodEntity g : goodEntities) {

            File goodImg = new File(AppConfig.DataFile.getOrderImgTempFilePath(g));
            File toGoodImg = new File(AppConfig.DataFile.getOrderImgUpFilePath(orderEntity, g));


            if(AppConfig.isOrderImgToSZ) {
                File toSZGoodImg = new File(AppConfig.DataFile.getOrderImgUpSZFilePath(orderEntity, g));
                try {
                    FileHelper.copyFileWithNio(goodImg, toSZGoodImg, null);
                } catch (Exception e) {
                }
            }

            if(AppConfig.isOrderImgToMe) {

                goodImg.renameTo(toGoodImg);
            }
        }


        FileHelper.deleteFileOrFolder(new File(AppConfig.DataFile.orderImgTempFileDir));

        isWriteToUp = false;
    }



    private synchronized static void deleteTemp(OrderEntity.GoodEntity goodEntity) {
        FileHelper.deleteFileOrFolder(new File(AppConfig.DataFile.getOrderImgTempFilePath(goodEntity)));
    }

    private synchronized static void deleteAllTemp() {
        FileHelper.deleteFolder(new File(AppConfig.DataFile.orderImgTempFileDir));
    }

    private synchronized static void save_(OrderEntity.GoodEntity goodEntity, byte[] data) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int cx = width/2;
        int cy = height/2;
        int offsex = cx / 3;
        int offsey = cy / 3;
        int offsex1 = offsex;
        int offsey1 = offsey;
        offsex = offsex/2;


        Matrix matrix = new Matrix();
        matrix.setRotate(180,width/2,width/2);

        Bitmap bitmap1 = Bitmap.createBitmap(bitmap,offsex,offsey,width - (offsex+offsex1),height - (offsey+offsey1) ,matrix,false);
        bitmap.recycle();
        bitmap = bitmap1;

        String xxsavePath = AppConfig.DataFile.getOrderImgTempFilePath(goodEntity);
        try {
            File ff = new File(xxsavePath);
            File ffp = ff.getParentFile();
            if (!ffp.exists()) {
                ffp.mkdirs();
            }
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, new FileOutputStream(ff));

            LinkBus.sendBroadcast(new OnGoodImageTackEvent(xxsavePath,goodEntity));
        } catch (Exception e) {
        }
    }

}
